package com.tjtt.common.utils;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class StringFormat {

	public static String formatValue(Object valueToFormat, String formatStr) {
		if (valueToFormat instanceof String) {
			return (String) valueToFormat;
		}
		if (valueToFormat instanceof Number) {
			return ((Number) valueToFormat).toString();
		}
		if (valueToFormat instanceof Byte) {
			return ((Byte) valueToFormat).toString();
		}
		if (valueToFormat instanceof Short) {
			return ((Short) valueToFormat).toString();
		}
		if (valueToFormat instanceof Integer) {
			return ((Integer) valueToFormat).toString();
		}
		if (valueToFormat instanceof Long) {
			return ((Long) valueToFormat).toString();
		}
		if (valueToFormat instanceof BigInteger) {
			return ((BigInteger) valueToFormat).toString();
		}
		if (valueToFormat instanceof Float) {
			return ((Float) valueToFormat).toString();
		}
		if (valueToFormat instanceof Double) {
			return ((Double) valueToFormat).toString();
		}
		if (valueToFormat instanceof BigDecimal) {
			return ((BigDecimal) valueToFormat).toString();
		}
		if (valueToFormat instanceof java.util.Date) {
			return ((BigDecimal) valueToFormat).toString();
		}
		if (valueToFormat instanceof java.util.Date) {
			return getDateString((java.util.Date) valueToFormat, formatStr);
		}
		if (valueToFormat instanceof java.sql.Date) {
			return valueToFormat.toString();
		}
		return valueToFormat.toString();
	}

	public static String formatValue(Object valueToFormat) {
		return formatValue(valueToFormat, null);
	}

	public static String getDateString(Date date, String pattern) {
		String formatString = "";
		if (pattern == null || pattern.equals("")) {
			// 默认格式
			pattern = "yyyy-MM-dd";
		}
		if (date != null) {
			// 日期不为空
			try {
				// 格式化日期工具类
				DateFormat df = new SimpleDateFormat(pattern);
				formatString = df.format(date);
			} catch (Throwable t) {
				t.printStackTrace();
			}
		}
		// 返回格式化的字符串
		return formatString;
	}

	/**
	 * 格式化中文日期
	 * 
	 * @param date
	 * @return
	 */
	public static String getCNDateString(Date date) {
		String formatString = "";
		DateFormat df = null; // 声明DateFormat对象
		if (date != null) {
			try {
				// 取得日期，并设置日期显示风格
				df = DateFormat.getDateInstance(DateFormat.YEAR_FIELD,
						new Locale("zh", "CN"));
				formatString = df.format(date);
			} catch (Throwable e) {
				e.printStackTrace();
			}
		}
		// 返回格式化的字符串
		return formatString;
	}

	/**
	 * 格式化中文时间
	 * 
	 * @param date
	 * @return
	 */
	public static String getCNTimeString(Date date) {
		String formatString = "";
		// 声明DateFormat对象
		DateFormat df = null;
		try {
			// 取得日期时间，设置日期的显示格式、时间的显示格式
			df = DateFormat.getDateTimeInstance(DateFormat.YEAR_FIELD,
					DateFormat.ERA_FIELD, new Locale("zh", "CN"));
			formatString = df.format(date);
		} catch (Throwable e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		// 返回格式化的字符串
		return formatString;
	}

	@SuppressWarnings("unchecked")
	public static List str2List(String str, String split) {
		List returnList = new ArrayList();
		String[] arr = str.split(split);
		for (int i = 0; i < arr.length; i++) {
			returnList.add(arr[i]);
		}
		return returnList;
	}

	public static String[] String2Array(String str, String split) {
		String[] arr = null;
		if (str == null || str.length() <= 0) {
			arr = new String[1];
			arr[0] = "";
		} else {
			arr = str.split(split);
		}
		return arr;
	}

	/**
	 * Ascii转换成String
	 * 
	 * @param asciiChars
	 *            char[]类型存放的Ascii值
	 * @return String
	 * @author 王明龙
	 */
	public static String ascii2String(char[] asciiChars) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < asciiChars.length; i++) {
			sb.append((int) asciiChars[i]);
		}
		return sb.toString();
	}

	/**
	 * Ascii转换成String
	 * 
	 * @param asciiStr
	 *            Ascii拼接的字符串
	 * @param pattern
	 *            拼接字符
	 * @return String
	 * @author 王明龙
	 */
	public static String ascii2string(String asciiStr, String pattern) {
		StringBuilder sb = new StringBuilder();
		String[] s = asciiStr.split(pattern);
		for (int i = 0; i < s.length; i++) {
			sb.append((char) Integer.parseInt(s[i]));
		}
		return sb.toString();
	}

	/**
	 * String转换成Ascii,返回由ascii按照分隔符拼接成字符串
	 * 
	 * @param str
	 * @param pattern
	 * @return String
	 * @author 王明龙
	 */
	public static String string2Ascii(String str, String pattern) {
		StringBuilder sb = new StringBuilder("");
		char[] chars = str.toCharArray();
		for (int i = 0; i < chars.length; i++) {// 输出
			if (!sb.toString().equals("")) {
				sb.append(pattern);
			}
			sb.append((int) chars[i]);
		}
		return sb.toString();
	}

	/**
	 * gbk转utf-8
	 * 
	 * @param gbk
	 * @return String
	 * @author 王明龙
	 */
	public static String gbk2utf8(String gbk) {
		String utf8 = "";
		try {
			utf8 = new String(gbk2utf8Byte(gbk), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return utf8;
	}

	/**
	 * gbk转utf-8
	 * 
	 * @param chenese
	 * @return byte[]
	 * @author 王明龙
	 */
	public static byte[] gbk2utf8Byte(String chenese) {
		char c[] = chenese.toCharArray();
		byte[] fullByte = new byte[3 * c.length];
		for (int i = 0; i < c.length; i++) {
			int m = (int) c[i];
			String word = Integer.toBinaryString(m);

			StringBuffer sb = new StringBuffer();
			int len = 16 - word.length();
			for (int j = 0; j < len; j++) {
				sb.append("0");
			}
			sb.append(word);
			sb.insert(0, "1110");
			sb.insert(8, "10");
			sb.insert(16, "10");

			String s1 = sb.substring(0, 8);
			String s2 = sb.substring(8, 16);
			String s3 = sb.substring(16);

			byte b0 = Integer.valueOf(s1, 2).byteValue();
			byte b1 = Integer.valueOf(s2, 2).byteValue();
			byte b2 = Integer.valueOf(s3, 2).byteValue();
			byte[] bf = new byte[3];
			bf[0] = b0;
			fullByte[i * 3] = bf[0];
			bf[1] = b1;
			fullByte[i * 3 + 1] = bf[1];
			bf[2] = b2;
			fullByte[i * 3 + 2] = bf[2];

		}
		return fullByte;
	}

	public static void main(String[] asgr) {
		String s = "123456789!@#$%^&*()_+哈哈喆|：；；;>。、";
		String asciiStr = StringFormat.string2Ascii(s, "/");
		System.out.println(asciiStr);
		String str = StringFormat.ascii2string(asciiStr, "/");
		System.out.println(s);
		System.out.println(str);
	}
}
